package com.bilibili.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bilibili.entity.CommenResult;
import com.bilibili.entity.Danmaku;
import com.bilibili.entity.Page;
import com.bilibili.entity.User;
import com.bilibili.service.DanmakuService;
import com.bilibili.service.impl.DanmakuServiceImpl;

@WebServlet("/danmaku.do")
public class DanmakuServlet extends HttpServlet {
	DanmakuService ds = new DanmakuServiceImpl();

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("application/json;charset=utf-8");

		// 获取分页参数，设置默认值
		int pageNum = Integer.parseInt(request.getParameter("pageNum") != null ? request.getParameter("pageNum") : "1");
		int pageSize = 5; // 固定每页显示5条

		// 获取筛选参数
		String keyword = request.getParameter("keyword") != null ? request.getParameter("keyword") : "";
		String timeRange = request.getParameter("timeRange") != null ? request.getParameter("timeRange") : "";
		String videoTitle = request.getParameter("videoTitle") != null ? request.getParameter("videoTitle") : "";

		int upId = ((User)request.getSession().getAttribute("loginUser")).getUserId(); // 假设当前UP主ID为1，实际应用中应从会话获取

		// 调用带筛选条件的分页查询
		Page<Danmaku> page = ds.selectDanmakuByPageWithFilters(upId, pageNum, pageSize, keyword, timeRange, videoTitle);

		// 返回分页结果
		CommenResult result = new CommenResult();
		result.setCode(200);
		result.setMessage("查询成功");
		result.setData(page);
		response.getWriter().write(result.toJOSNString());
	}

	

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
